package com.xxm.thoughtworksdemo

import android.content.Context
import android.graphics.Color
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.TextUtils
import android.text.style.ForegroundColorSpan
import android.util.TypedValue


fun dp2px(context: Context, dpValue: Float): Int {
    return TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_DIP,
        dpValue, context.resources.displayMetrics
    )
        .toInt()
}

fun getScreenWidth(context: Context): Int {
    return context.resources.displayMetrics.widthPixels
}

fun getScreenHeight(context: Context): Int {
    return context.resources.displayMetrics.heightPixels
}

fun makeSingleCommentSpan(userName: String, content: String?): SpannableStringBuilder {
    val richText = String.format("%s: %s", userName, content)
    val builder = SpannableStringBuilder(richText)
    if (!TextUtils.isEmpty(userName)) {
        val foregroundColorSpan = ForegroundColorSpan(Color.parseColor("#0000FF"))
        builder.setSpan(
            foregroundColorSpan,
            0,
            userName.length,
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        )
    }
    return builder
}